package com.cgtong.android.adapter;

import android.content.Context;
import android.support.annotation.IntDef;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import com.cgtong.android.R;
import com.cgtong.android.activity.LocalImagePreviewActivity;
import com.cgtong.android.bean.ProblemBean;
import com.cgtong.android.widget.GlideApp;
import com.chad.library.adapter.base.BaseViewHolder;
import com.github.vipulasri.timelineview.LineType;
import com.github.vipulasri.timelineview.TimelineView;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 步骤进度适适配器
 * Created by chunc on 2017/10/19.
 */

public class VerticalStepAdapter extends RecyclerView.Adapter<VerticalStepAdapter.StepViewHolder> {
    public static final int Vertical = 1;
    public static final int Horizontal = 2;

    @IntDef({Vertical, Horizontal})
    @Retention(RetentionPolicy.SOURCE)
    public @interface StepOrientation {
    }

    private Context mContext;
    private LayoutInflater layoutInflater;
    private int stepOrientation;
    private List<ProblemBean.StatusChangeBean> modelList;
    private String[] stepArray;
    private List<ProblemBean.ResourceBean> resourceBeanList;

    public void setPushImageBeanList(List<ProblemBean.PushImageBean> pushImageBeanList) {
        resourceBeanList = new ArrayList<>();
        for (ProblemBean.PushImageBean bean : pushImageBeanList) {
            ProblemBean.ResourceBean resourceBean = new ProblemBean.ResourceBean(bean.getPicName());
            resourceBeanList.add(resourceBean);
        }
    }

    public VerticalStepAdapter(Context context, @Nullable List<ProblemBean.StatusChangeBean> data, @StepOrientation int stepOrientation) {
        this.mContext = context;
        this.modelList = data;
        this.stepOrientation = stepOrientation;
        layoutInflater = LayoutInflater.from(mContext);
        stepArray = context.getResources().getStringArray(R.array.vertical_step_array);
    }

    @Override
    public int getItemViewType(int position) {
        return TimelineView.getTimeLineViewType(position, getItemCount());
    }

    @Override
    public StepViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view;
        if (stepOrientation == Horizontal) {
            view = layoutInflater.inflate(R.layout.layout_horizontal_step, parent, false);
        } else {
            view = layoutInflater.inflate(R.layout.layout_vertical_step, parent, false);
        }
        return new StepViewHolder(view, viewType);
    }

    public ProblemBean.StatusChangeBean getItem(int position) {
        if (position <= modelList.size() - 1) {
            return modelList.get(position);
        }
        return null;
    }

    @Override
    public void onBindViewHolder(StepViewHolder holder, int position) {
        holder.stepTxt.setText(stepArray[position]);
        ProblemBean.StatusChangeBean item = getItem(position);
        if (item != null) {
            holder.msgTxt.setText(item.getDesc());
            holder.dateTxt.setText(item.getCreateTime());

            holder.stepTxt.setTextColor(ContextCompat.getColor(mContext, R.color.color_order_complete));
            holder.timelineView.setMarker(ContextCompat.getDrawable(mContext, R.drawable.ic_attention_dot));

            if (position == 0) {
                holder.timelineView.setEndLine(ContextCompat.getColor(mContext, R.color.color_order_complete), LineType.BEGIN);
            } else if (position == 3) {
                holder.timelineView.setStartLine(ContextCompat.getColor(mContext, R.color.color_order_complete), LineType.END);
                //显示结案图片
                if (resourceBeanList != null && resourceBeanList.size() > 0) {
                    holder.gridView.setVisibility(View.VISIBLE);
                    holder.gridView.setAdapter(new PictureAdapter(resourceBeanList));
                }
            } else {
                holder.timelineView.setStartLine(ContextCompat.getColor(mContext, R.color.color_order_complete), LineType.NORMAL);
                holder.timelineView.setEndLine(ContextCompat.getColor(mContext, R.color.color_order_complete), LineType.NORMAL);
            }
        } else {
            if (position == 3) {
                holder.msgTxt.setVisibility(View.GONE);
                holder.dateTxt.setVisibility(View.GONE);

            }
        }
    }

    @Override
    public int getItemCount() {
        return 4;
    }


//    private String format(String content, String date) {
//        content = TextUtils.isEmpty(content) ? "" : content;
//        return "<font color=\"#54BB50\">" + content + "</font>&#160;&#160;" + date;
//    }

    class StepViewHolder extends BaseViewHolder {

        @BindView(R.id.step_txt)
        TextView stepTxt;
        @BindView(R.id.date_txt)
        TextView dateTxt;
        @BindView(R.id.msg_txt)
        TextView msgTxt;
        @BindView(R.id.time_marker)
        TimelineView timelineView;
        @BindView(R.id.grid_view)
        GridView gridView;

        public StepViewHolder(View itemView, int viewType) {
            super(itemView);
            ButterKnife.bind(this, itemView);
            timelineView.initLine(viewType);
        }
    }

    //--------------【已结案】存在图片，最多三张-----------------
    class PictureAdapter extends BaseAdapter {

        private LayoutInflater mLayoutInflater;
        private List<ProblemBean.ResourceBean> mList;

        public PictureAdapter(List<ProblemBean.ResourceBean> mList) {
            this.mList = mList;
            mLayoutInflater = LayoutInflater.from(mContext);
        }

        @Override
        public int getCount() {
            return 3;
        }

        @Override
        public ProblemBean.ResourceBean getItem(int position) {
            if (position <= mList.size() - 1) {
                return mList.get(position);
            }
            return null;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            if (convertView == null) {
                convertView = mLayoutInflater.inflate(R.layout.layout_finished_picture, parent, false);
                holder = new ViewHolder(convertView);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            ProblemBean.ResourceBean item = getItem(position);
            if (item != null) {
                GlideApp.with(mContext).load(getItem(position).getContent()).dontAnimate()
                        .placeholder(R.drawable.icon_default_loading)
                        .error(R.drawable.icon_default_error).into(holder.imageView);

                holder.imageView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        LocalImagePreviewActivity.start(mContext, mList, position);

                    }
                });
            }
            return convertView;
        }


        class ViewHolder {

            @BindView(R.id.img_view)
            ImageView imageView;

            ViewHolder(View view) {
                ButterKnife.bind(this, view);
            }
        }

    }
}
